package cn.com.xy.sms.sdk.db.entity;

import java.util.Map;

import org.json.JSONObject;

import android.content.ContentValues;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.db.DBManager;
import cn.com.xy.sms.sdk.db.XyCursor;
import cn.com.xy.sms.sdk.db.base.BaseManager;
import cn.com.xy.sms.sdk.log.LogManager;
/* SDK-529 huangzhiqiang 20160711 start */
import cn.com.xy.sms.sdk.util.StringUtils;
/* SDK-529 huangzhiqiang 20160711 end */

public class OperatorParseManager {

    public static final String TABLE_NAME = "tb_operator_parse_info";
    
    public static final String ID = "id";
    
    public static final String PHONE = "phone";
    
    public static final String MSG = "msg";
    
    public static final String RESULT = "result";
    
    public static final String UPDATE_INFO_TIME = "updateInfoTime";

    /* SDK-529 huangzhiqiang 20160711 start */
    public static final String NUM_MSG_MD5 = "numMsgMD5";
    /* SDK-529 huangzhiqiang 20160711 end */
    
    private static String[] mColumns = new String[] {UPDATE_INFO_TIME,RESULT};

    /* SDK-529 huangzhiqiang 20160711 start */
    public static final String CLEAR_MSG_FIELD_DATA = "UPDATE " + TABLE_NAME + " SET " + MSG + "=NULL";
    public static final String ADD_NUM_MSG_MD5 = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + NUM_MSG_MD5 + " TEXT";
    /* SDK-529 huangzhiqiang 20160711 end */

    public static String getCreateTableSql() {
        return " create table if not exists " + TABLE_NAME + " ("
                + ID+" INTEGER PRIMARY KEY,"           
                + PHONE +" TEXT," + MSG +" TEXT," + NUM_MSG_MD5 +" TEXT," + RESULT +" TEXT,"
                + UPDATE_INFO_TIME+"  long DEFAULT '0')";
    }
    
    
    public static JSONObject queryOperatorMsg(String phoneNum, String msg,Map<String, String>  extend){
    	JSONObject jsonObject = null;
          XyCursor cur = null;
          try {
              String numMsgMD5 = StringUtils.getMD5(phoneNum + msg);
              cur = DBManager.query(TABLE_NAME,mColumns, PHONE + " = ? and " + NUM_MSG_MD5
                      + " = ? ", new String[] { phoneNum, numMsgMD5 }, null, null,
                      null, null);
              jsonObject = BaseManager.loadSingleDataFromCursor(mColumns, cur);
          } catch (Throwable e) {
              /*SDK-550 hewengao 20160719 start*/
              LogManager.w(Constant.TAG, "queryOperatorMsg: ", e);
              /*SDK-550 hewengao 20160719 end*/
          } finally {
              XyCursor.closeCursor(cur, true);
          }
          return jsonObject;
    }

    public static long insertOrUpdateOperatorMsg(String phoneNum, String msg, String result) {
        /* SDK-529 huangzhiqiang 20160711 start */
        if (!StringUtils.allValuesIsNotNull(phoneNum, msg)) {
            return -1;
        }
        /* SDK-529 huangzhiqiang 20160711 end */
        long intsertResult = -1;
        ContentValues values = null;
        try {
            /* SDK-529 huangzhiqiang 20160711 start */
            String numMsgMD5 = StringUtils.getMD5(phoneNum + msg);
            values = BaseManager.getContentValues(null, PHONE, phoneNum, NUM_MSG_MD5, numMsgMD5, RESULT, result, UPDATE_INFO_TIME,
                    String.valueOf(System.currentTimeMillis()));
            /*HWWATCH-4 songzhirong 20160818 start*/
            int rows = DBManager.update(TABLE_NAME, values, PHONE + " = ? and " + NUM_MSG_MD5 + " = ?",
                    new String[] { phoneNum, numMsgMD5 });
            /*HWWATCH-4 songzhirong 20160818 end*/
            /* SDK-529 huangzhiqiang 20160711 end */
            if (rows <= 0) {
                intsertResult = DBManager.insert(TABLE_NAME, values);
            } else {
                intsertResult = 0;
            }
        } catch (Throwable e) {
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "insertOrUpdateOperatorMsg: ", e);
            /*SDK-550 hewengao 20160719 end*/
        }
        return intsertResult;
    }
}
